//IntegerPanel.java
//Timothy Grant
//CS1410
//Calculator 1410
//Final Version
package calculator1410;

import javax.swing.JFrame;
import javax.swing.JTextArea;
import numbers.IntegerNumber;
import evaluateexpression.ExpressionEvaluator;

public class IntegerPanel extends NumberPanel {

    @Override
    public void toggleInfo() {
        String helpMessage = "The following operations are supported: \n\n"
                + "     Addition: 3+4 = 7\n"
                + "     Subtraction: 6-3 = 3\n"
                + "     Multiplication: 2*4 = 8\n"
                + "     Division: 24/3 = 8\n"
                + "     Remainder: 7%3 = 1\n\n"
                + "Please use only whole numbers.\n\n"
                + "Parentheses are supported: (1+2)-(3*4)= -9";


        String helpHeader = "Integer Number Help";

        JFrame helpFrame = new JFrame(helpHeader);
        JTextArea helpExp = new JTextArea(helpMessage);
        //helpExp.setFont( helpFont );
        helpFrame.add(helpExp);
        helpFrame.setResizable(false);
        helpFrame.pack();
        //helpFrame.setSize(300, 240);
        helpFrame.setVisible(true);
    }

    @Override
    public void equals() {
        IntegerNumber integer = new IntegerNumber(0);
        String equal;
        if (!expressionArea.getText().contains("=")) {
            equal = ExpressionEvaluator.evalExpression(
                    expressionArea.getText(), integer);
            expressionArea.setText(expressionArea.getText() + "\n=\n" + equal);
        } else {
            int equalIndex = expressionArea.getText().indexOf("=");
            equal = ExpressionEvaluator.evalExpression(
                    expressionArea.getText().substring(++equalIndex), integer);
            expressionArea.setText(expressionArea.getText().substring(++equalIndex)
                    + "\n=\n" + equal);
        }
    }

    @Override
    public void newEquals() {
        IntegerNumber integer = new IntegerNumber(0);
        String equal;
        if (!expressionArea.getText().contains("=")) {
            equal = ExpressionEvaluator.evalExpression(
                    expressionArea.getText(), integer);
            expressionArea.setText(expressionArea.getText() + "\n=\n" + equal);
        } else {
            int equalIndex = expressionArea.getText().indexOf("=");
            equal = ExpressionEvaluator.evalExpression(
                    expressionArea.getText().substring(++equalIndex), integer);
            expressionArea.setText(expressionArea.getText().substring(++equalIndex)
                    + "\n=\n" + equal);
        }
    }
}
